package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.goods.entity.GoodsAttributeContentEntity;
import com.yuanfeng.goods.entity.GoodsAttributeEntity;
import com.yuanfeng.goods.entity.GoodsBrandCatEntity;
import com.yuanfeng.goods.mapper.GoodsAttributeContentMapper;
import com.yuanfeng.goods.mapper.GoodsAttributeMapper;
import com.yuanfeng.goods.mapper.GoodsBrandCatMapper;
import com.yuanfeng.goods.service.GoodsAttributeService;
import com.yuanfeng.goods.vo.GoodsAttributeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsBrandCatServiceImpl
 * @Description  商品品牌分类表
 * @Author cyp
 * @Date 2023-06-26 13:50:11
 * @Version 1.0
 **/
@Service
public class GoodsAttributeServiceImpl extends ServiceImpl<GoodsAttributeMapper, GoodsAttributeEntity>
        implements GoodsAttributeService {

    @Autowired
    private GoodsAttributeContentMapper goodsAttributeContentMapper;

    @Autowired
    private GoodsBrandCatMapper goodsBrandCatMapper;

    @Override
    public ResponseResult selectCatAttribute(Map<String, Object> map) {
        if (null != map && null != map.get("catId")) {
            Map<String,Object> result = new HashMap<>();

            List<GoodsAttributeVO> volist = new ArrayList<>();

            List<GoodsAttributeEntity> goodsAttributeResult=this.baseMapper.selectList(new LambdaQueryWrapper<GoodsAttributeEntity>()
                                                            .eq(GoodsAttributeEntity::getCatId,Integer.valueOf(map.get("catId").toString())));
            if(!goodsAttributeResult.isEmpty()) {
                for (GoodsAttributeEntity goodsAttriResult : goodsAttributeResult) {
                    GoodsAttributeVO goodsAttResult =new GoodsAttributeVO();
                    List<GoodsAttributeContentEntity> AttrContentResult = goodsAttributeContentMapper.selectList(new LambdaQueryWrapper<GoodsAttributeContentEntity>()
                                                                        .eq(GoodsAttributeContentEntity::getAttributeContentId,goodsAttriResult.getId()));
                    List<String> list = new ArrayList<>();
                    for (GoodsAttributeContentEntity AttriContentRes : AttrContentResult) {
                        list.add(AttriContentRes.getAttributeContent());
                    }
                    goodsAttResult.setAttributeContent(list);
                    goodsAttResult.setId(goodsAttriResult.getId());
                    goodsAttResult.setCommodityAttribute(goodsAttriResult.getCommodityAttribute());
                    goodsAttResult.setAttributeStatus(goodsAttriResult.getAttributeStatus());

                    volist.add(goodsAttResult);
                    result.put("goodsAttResult",volist);
                }
            }
            List<GoodsBrandCatEntity> goodsBrand =  goodsBrandCatMapper.selectList(new LambdaQueryWrapper<GoodsBrandCatEntity>()
                    .eq(GoodsBrandCatEntity::getCatId,Integer.valueOf(map.get("catId").toString()))
                    .groupBy(GoodsBrandCatEntity::getBrandId));
            if(!goodsBrand.isEmpty()){
                result.put("goodsBrand",goodsBrand);
            }
            return ResponseResult.success(result);
        }
        return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER);
    }

    @Override
    public ResponseResult queryBrandCatAttributeList(Map<String, Object> map) {
        Integer catId =(Integer)map.get("catId");
        String attributeContent =(String)map.get("attributeContent");




        List<GoodsAttributeEntity> goodsAttributeResult=this.baseMapper.selectList(new LambdaQueryWrapper<GoodsAttributeEntity>()
                                                        .eq(GoodsAttributeEntity::getCatId,catId));
        if(goodsAttributeResult.size()>0) {
            for (GoodsAttributeEntity goodsAttriResult : goodsAttributeResult) {
                List<GoodsAttributeContentEntity> attrContentList = goodsAttributeContentMapper.selectList(new LambdaQueryWrapper<GoodsAttributeContentEntity>()
                                                                    .eq(GoodsAttributeContentEntity::getAttributeContentId,goodsAttriResult.getId())
                                                                    .like(GoodsAttributeContentEntity::getAttributeContent,attributeContent));

                goodsAttriResult.setAttrContentList(attrContentList);
            }

            return ResponseResult.success(goodsAttributeResult);
        }else{
            return ResponseResult.fail(BizCodeEnume.NO_INFORMATION_OBTAINED);
        }

    }
}
